package vista;

import javax.swing.*;
import java.awt.*;

public class VJButtonImg extends JButton implements java.io.Serializable{

	private Image image=null;
	private Icon icon;
	private String text,titulo;

    public VJButtonImg() {
		propies();
    }

    public VJButtonImg(String d,String t){
		ImageIcon icon = new ImageIcon(d,t);
		setImage(icon);
		text=t;
		propies();
    }

	/*public JButtonImg(String d,String txt,String t){
		ImageIcon icon = new ImageIcon(d);
		setImage(icon);
		text=txt;
		titulo=t;
		propies();
        setBorderPainted(true);
	}*/

	private void propies(){
        //setForeground(Color.BLACK);
        setOpaque(false);
        setContentAreaFilled(false);
        setFocusPainted(false);
        setBorderPainted(false);
		setToolTipText(text);

		if(titulo!=null)
			setPreferredSize(new Dimension(image.getWidth(null)+70,image.getHeight(null)+2));
		else
			setPreferredSize(new Dimension(image.getWidth(null)+2,image.getHeight(null)+2));
	}

    protected void paintComponent(Graphics g) {
		int x=image.getWidth(null),y=image.getHeight(null),posx=(int)(getWidth()-x)/2,posy=(int)(getHeight()-x)/2;

        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

        ButtonModel m = getModel();

			if(titulo!=null)
				g2.setFont(new Font("Ebrima",0,11));

        if (m.isPressed()){//al hacer click
			x=image.getWidth(null)-2;
			y=image.getHeight(null)-2;

			g2.setColor(new Color(150,150,150));
			g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.3f));
			g2.fillRoundRect(0,0,getWidth()-1, getHeight()-1,10,10);
			g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1.0f));
			g2.setColor(Color.BLACK);

			if(titulo!=null){
				g2.drawString(titulo,x+14,17);
				posx=5;
			}

			//Grosor del border
			g2.setStroke (new BasicStroke(1,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND));
			//Color del border
			g2.setColor(new Color(60,127,177));
			g2.drawRoundRect(0,0,getWidth()-1, getHeight()-1,10,10);

			g2.drawImage(getImage(),posx,posy,x,y, null);
        } else if(m.isRollover()){//Cuando pasa el mouse
			x=image.getWidth(null);
			y=image.getHeight(null);

			g2.setColor(new Color(140,140,140));
			g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.1f));
			g2.fillRoundRect(0,0,getWidth()-1, getHeight()-1,10,10);
			g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1.0f));
			g2.setColor(Color.BLACK);

			if(titulo!=null){
				g2.drawString(titulo,x+10,16);
				posx=5;
			}

			//Grosor del border
			g2.setStroke (new BasicStroke(1,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND));
			//Color del border
			g2.setColor(new Color(60,127,177));
			g2.drawRoundRect(0,0,getWidth()-1, getHeight()-1,10,10);


			g2.drawImage(getImage(),posx,posy,x,y, null);
        }else{//Si no hay nada; image.getWidth(null)
			x=image.getWidth(null);
			y=image.getHeight(null);

        	if (!m.isEnabled())
				g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.4f));

			if(titulo!=null){
				g2.drawString(titulo,x+10,16);
				//Grosor del border
				g2.setStroke (new BasicStroke(1,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND));
				//Color del border
				g2.setColor(new Color(60,127,177));
				g2.drawRoundRect(0,0,getWidth()-1, getHeight()-1,10,10);
				posx=5;
			}

			g2.drawImage(getImage(),posx,posy,x,y, null);

		}

        super.paintComponent(g);
    }

	public Image getImage() {
		return image;
	}

	public void setImage(ImageIcon i) {
		image=i.getImage();
	}

	/*public Icon getIcon() {
		return icon;
	}

	public void setIcon(Icon icon){
		this.icon=icon;
		setImage(((ImageIcon)icon).getImage());
	}*/
}

